# ========================================================================
# 顾及指标时空统一性计算指标权重和评价得分
# ========================================================================
import numpy as np
import pandas as pd

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from get_proportion import get_proportion
from get_weight_by_entropy import get_weight_by_entropy
from get_weight_by_msd import get_weight_by_msd
from load_dataframe import load_dataframe
from normalizing import normalizing


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
# 城市：昆明市，曲靖市，玉溪市，楚雄州，红河州
# 指标类型：新型城镇化，生态环境
zb_type = '新型城镇化'
zb_file = 'data/年鉴统计数据/地级市数据/地级市指标分类体系.xlsx'
years = [2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019]
index = pd.Series(years)
df_w = pd.DataFrame()  # 所有权重集合
df_sumscore = pd.DataFrame()  # 所有综合得分
# 加载指标
df_zb = pd.read_excel(zb_file, sheet_name=zb_type)
cxzb_names = np.array(df_zb.columns)
# 加载州市
df_city = pd.read_excel(zb_file, sheet_name='州市')
city_names = np.array(df_city.columns)
df_allcity = {}
for cx_name in cxzb_names:
    df_allcity[cx_name] = pd.DataFrame()
# 读取所有州市数据
for city_name in city_names:
    excel_name = zb_type + '-' + city_name
    csh_file = 'data/年鉴统计数据/地级市数据/最终成果/' + excel_name + '.xlsx'
    # 读取所有抽象分类层指标数据
    for cx_name in cxzb_names:
        # 加载数据
        dataframe = load_dataframe(csh_file, cx_name)
        # 合并同类项
        df_allcity[cx_name] = df_allcity[cx_name].append(dataframe, ignore_index=True)
print('=======================合并同类项=======================')
print(df_allcity)
with pd.ExcelWriter(zb_type + '-归一化处理-计算权重.xlsx') as writer:
    for cx_name in df_allcity:
        sub_fd = df_allcity[cx_name]
        sub_fd.to_excel(writer, sheet_name=cx_name)
        # 归一化处理
        normal_df = normalizing(sub_fd)
        normal_df.to_excel(writer, sheet_name=cx_name + '-归一化')
        # 求单个数值占整个序列数值之和的比重
        proportion_df = get_proportion(normal_df)
        # 计算指标权重
        df_weight = pd.DataFrame()
        # 熵权法确定指标权重
        weight_ent = get_weight_by_entropy(proportion_df)
        df_weight = df_weight.append(weight_ent)
        # 均方差决策法确定指标权重
        weight_msd = get_weight_by_msd(proportion_df)
        df_weight = df_weight.append(weight_msd)
        # 求综合权重
        weights = 0.5 * (weight_ent + weight_msd)
        weights.name = '综合权重'
        print('----------综合权重----------')
        print(weights)
        df_weight = df_weight.append(weights)
        print('----------权重----------')
        print(df_weight)
        df_weight_T = pd.DataFrame(df_weight.values.T, columns=df_weight.index, index=df_weight.columns)
        df_weight_T = df_weight_T.round(2)
        df_weight_T.to_excel(writer, sheet_name=cx_name + '-权重')
        df_w = df_w.append(df_weight_T)
        # 计算指标得分
        scores_df = pd.DataFrame(columns=normal_df.columns, index=normal_df.index)
        for i in weights.index:
            scores_df[i] = weights[i] * normal_df[i]
        N = scores_df.shape[1]
        # 计算指标综合得分
        scores_df['综合得分'] = scores_df.sum(axis=1)
        print('----------指标得分----------')
        print(scores_df)
        scores_df.to_excel(writer, sheet_name=cx_name + '-指标得分')
        df_sumscore[cx_name] = scores_df['综合得分']/N
print('----------综合权重----------')
print(df_w)
df_w.to_excel(zb_type + '-综合权重.xlsx')

# 同占比求综合得分
df_sumscore['综合得分'] = df_sumscore.sum(axis=1)
df_sumscore.round(3)
print('----------综合得分----------')
print(df_sumscore)
df_sumscore.to_excel(zb_type + '-综合得分.xlsx')

df_dz = pd.DataFrame(index=index)
step = 15
with pd.ExcelWriter(zb_type + '-各城市综合得分.xlsx') as writer2:
    for i in range(len(city_names)):
        name = city_names[i]
        start = i * 15
        end = start + step
        df_city_score = df_sumscore.iloc[start:end]
        # df_city_score2=df_city_score.rename(index=index)
        df_city_score2 = pd.DataFrame(df_city_score.values, index=index, columns=df_sumscore.columns)
        print(name + '----------综合得分----------')
        print(df_city_score2)
        df_city_score2.to_excel(writer2, sheet_name=name)
        df_dz[name] = df_city_score2['综合得分']
print('----------滇中各城市' + zb_type + '水平----------')
print(df_dz)
df_dz.to_excel('滇中各城市-' + zb_type + '水平.xlsx')
